package mobi.hami.huapin.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import mobi.hami.huapin.dao.OpinionLeaderMapper;
import mobi.hami.huapin.dao.ProductMapper;
import mobi.hami.huapin.dto.PageDTO;
import mobi.hami.huapin.model.OpinionLeader;
import mobi.hami.huapin.model.OpinionLeader;
import mobi.hami.huapin.model.OpinionLeaderExample;
import mobi.hami.huapin.model.ProductExample;
import mobi.hami.huapin.model.base.PageQO;
import mobi.hami.huapin.model.base.PageVO;
import mobi.hami.huapin.service.OpinionLeaderService;
import mobi.hami.huapin.util.CommonPage;
import mobi.hami.huapin.util.SearchListUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Service;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;

/**
 * 类说明
 *
 * @author 阿飞 jijunjian@hami.mobi
 * @date 2018/8/24 16:01
 */

@Service
public class OpinionLeaderServiceImpl implements OpinionLeaderService {

    private static final Logger logger = LoggerFactory.getLogger(OpinionLeaderServiceImpl.class);

    @Autowired
    JdbcTemplate jdbcTemplate;

    @Autowired
    private OpinionLeaderMapper productMapper;
    /**
     * 修改商品状态
     *
     * @param product
     * @return
     */
    public  int  updateDelete (OpinionLeader product){

        return    productMapper.updateDelete( product);
    }
    /**
     * 保存商品
     *
     * @param product
     * @return
     */
    @Override
    public Long save(OpinionLeader product) {
         productMapper.insertSelective(product);

        return product.getId();
    }

    /**
     * 更新商品
     *
     * @param product
     * @return
     */
    @Override
    public int update(OpinionLeader product) {


        return  productMapper.updateByPrimaryKeySelective(product);

    }

    /**
     * 更新商品
     *
     * @param spuid
     * @return
     */
    @Override
    public int delete(Long spuid) {

        OpinionLeader product = new OpinionLeader();
        product.setId(spuid);


        return productMapper.delete(product);
    }

    /**
     * 查询一个商品
     *
     * @param spuid
     * @return
     */
    @Override
    public OpinionLeader getOney(Long spuid) {
        OpinionLeader product = new OpinionLeader();
        product.setId(spuid);
        return  productMapper.selectOne(product);
    }


    /**
     * 查询条数
     *
     * @param where
     * @return
     */
    @Override
    public int getCount(String where) {

        String  sql  =   "SELECT COUNT(1) from  product \n" +
                "    WHERE 1=1 "+ where;

        int rs  = jdbcTemplate.queryForObject(sql,null,Integer.class);

        return rs;
    }


    @Override
    public PageVO<OpinionLeader> getList( Map<String,Object> reqMap) {

        OpinionLeaderExample example = new OpinionLeaderExample();
        OpinionLeaderExample.Criteria criteria = example.createCriteria();

        criteria.andDeletedEqualTo(0);
        if (reqMap.containsKey("spu_id") && reqMap.get("spu_id").toString().length() > 0)
        {
            criteria.andSpu_idEqualTo(  Integer.parseInt(reqMap.get("spu_id").toString()));
        }

        PageQO<?> pageQO = SearchListUtil.resolveSearchData(reqMap);

        example.setOrderByClause(pageQO.getOrderBy());

        PageHelper.startPage(pageQO.getPageNum(), pageQO.getPageSize());
        List<OpinionLeader> list = productMapper.selectByExample(example);

        PageInfo<OpinionLeader> pages=new PageInfo<>(list);


        PageVO vo = PageVO.build(list);
        vo.setPages(pageQO.getPageNum());
        vo.setTotalCount((long)( pages.getTotal()));
        vo.setPages(pageQO.getPageSize());

        //logger.info("条数",pages.getTotal());

        return vo;
    }

    /**
     * 查询条数
     *
     * @param reqMap
     * @return
     */
    @Override
    public int getCount(Map<String, Object> reqMap) {

        ProductExample example = new ProductExample();
        ProductExample.Criteria criteria = example.createCriteria();
        criteria.andSubject_idEqualTo(9);

        int count = productMapper.selectCountByExample(example);

        return count;
    }



}
